package com.cyh.core.framework.common.utils;

import com.cyh.core.framework.domain.entity.Dictionary;
import com.cyh.core.framework.repository.dictionary.DictionaryRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @Author: yanhang_chen
 * @Date: 2020/9/4 16:52
 */
@Component
public class DictionaryUtil {

    private static DictionaryRepository dictionaryRepository;

    @Autowired
    private void set (DictionaryRepository dict){
        dictionaryRepository = dict;
    }

    public static String getDetail(String kind,String code){
        return dictionaryRepository.findByKindAndCode(kind, code).get().getDetail();
    }

    public static Dictionary getDictionary(String kind,String code){
        return dictionaryRepository.findByKindAndCode(kind, code).get();
    }

    public static Dictionary getDictionaryById(String id){
        return dictionaryRepository.findById(id).get();
    }

    public static List<Dictionary> getDictionaryByKind(String kind){
        return dictionaryRepository.findByKind(kind);
    }

    public static List<Dictionary> getWholeDictionary(){
        return dictionaryRepository.findAll();
    }

    public static void deleteOne(Dictionary dictionary){
        dictionaryRepository.delete(dictionary);
    }

    public static void delete(Iterable<Dictionary> dictionarys){
        if(dictionarys!=null) {
            dictionaryRepository.deleteInBatch(dictionarys);
        }
    }

    public static void updateOne(Dictionary dictionary) throws Exception {
        if("".equals(dictionary.getId())||dictionary.getId()==null){
            throw new NullPointerException("字典Id不能为空");
        }else {
            Dictionary d=null;
            try {
                d=getDictionaryById(dictionary.getId());
            }catch (Exception e){
                d=null;
            }
            if(d==null){
                throw new Exception("字典Id不存在,请使用插入字典方法");
            }else {
                if(dictionary.getDetail()!=null&&dictionary.getCode()!=null&&dictionary.getKind()!=null){
                    dictionary.setId(null);
                    dictionaryRepository.save(dictionary);
                }else {
                    throw new Exception("请补充完整除Id外的字典项再更新");
                }
            }
        }
    }

    public static void updateAll(Iterable<Dictionary> dictionarys) throws Exception{
        if (dictionarys!=null){
            dictionarys.forEach(d->{
                try {
                    updateOne(d);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            });
        }
    }

    public static void insertOne(Dictionary dictionary) throws Exception {
        if(dictionary!=null){
            if(dictionary.getDetail()!=null&&dictionary.getCode()!=null&&dictionary.getKind()!=null){
                dictionary.setId(null);
                Dictionary d=null;
                try {
                    d=getDictionary(dictionary.getKind(),dictionary.getCode());
                }catch (Exception e){
                    d=null;
                }
                if(d==null) {
                    dictionaryRepository.save(dictionary);
                }else {
                    throw new Exception("已存在Kind和Code");
                }
            }else {
                throw new Exception("请补充完整除Id外的字典项再插入");
            }
        }
    }

    public static void insertAll(Iterable<Dictionary> dictionarys) throws Exception{
        if (dictionarys!=null){
            dictionarys.forEach(d->{
                try {
                    insertOne(d);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            });
        }
    }
}
